﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Web.Caching;

using ProjectBase.Core;
using ProjectBase.Data.Xml;

namespace ProjectBase.Data.Db
{
    /// <summary>
    ///     Sometimes is not easy to Clear the cache.
    ///     When there are only StoredProcedures available,
    ///     it would be nice to connect caching to some common Cleaner.
    ///     This class provides the 'Dependency' property returning the 'CacheDependency'
    ///     which is connected to the empty file DbCache.xml.
    ///     This files is saved and therefore change whenever any ADO.NET Dao executes transaction.
    ///     This could be nice way for you how to handle Caching without direct access to database.
    /// </summary>
    public class DbCache : XmlDao
    {
        #region singleton
        static readonly DbCache DbCacheInstance = new DbCache();
        protected DbCache() { }

        protected static DbCache Instance
        {
            get { return DbCacheInstance; }
        }
        #endregion singleton

        #region abstract implemented (.xml DataSource)
        /// <summary>
        ///     The full pathe to the file, which acts as the cache dependency file
        /// </summary>
        protected override string DataSourcePath
        {
            get { return Global.CreateFilePath(Global.Settings.FileCacheDbPath); }
        }
        #endregion abstract implemented (.xml DataSource)

        #region protected Create, Clear
        /// <summary>
        ///     Creates new instances of the Dependency objects targeting the underlying .xml file
        /// </summary>
        /// <returns></returns>
        protected virtual CacheDependency CreateDependencyObject()
        {
            return new CacheDependency(DataSourcePath);
        }

        /// <summary>
        ///     Simply saves the underlying .xml file - the observers will then invalidate the cached elements
        /// </summary>
        protected virtual void ChangeTheCacheFile()
        {
            try
            {
                var f = new FileInfo(DataSourcePath);
                if (f.Exists)
                {
                    f.LastWriteTime = DateTime.Now;
                }
            }
            catch (Exception ex)
            {
                PublishingManager.Publish(this, "Cannot change cache file", ex);
            }
        }
        #endregion protected Create, Clear

        #region public static
        /// <summary>
        ///     Gets the new instance of the 'CacheDependency', which is related to the 'DbCache.xml' file.
        ///     When this file is changed, cache is cleared.
        /// </summary>
        public static CacheDependency Dependency
        {
            get { return Instance.CreateDependencyObject(); }
        }

        /// <summary>
        ///     Clears the cache. This will change the underlying .xml file,
        ///     which will cause invalidating of the cached objects
        /// </summary>
        protected internal static void Clear()
        {
            Instance.ChangeTheCacheFile();
        }
        #endregion public static
    }
}